/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shishuo.cms.constant.FolderConstant;
import com.shishuo.cms.entity.Article;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.vo.ArticleVo;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.exception.FolderNotFoundException;

/**
 * @author Herbert
 * 
 */
@Controller
@RequestMapping("/article")
public class ArticleAction extends BaseAction {

	/**   
	* 描述：   
	* 创建时间：2015年6月8日 下午3:43:04   
	* 修改备注：   YangJ  邮箱:yangjie@sxw100.com  
	* @version    
	*    
	*/ 
	@RequestMapping(value = "/{articleId}.htm", method = RequestMethod.GET)
	public String article(@PathVariable long articleId,
			@RequestParam(value = "p", defaultValue = "1") long p,
			ModelMap modelMap) {
		try {
			ArticleVo article = fileService.getArticleById(articleId);
			Folder folder = folderService.getFolderById(article.getFolderId());
			modelMap.addAttribute("p", p);
			modelMap.addAttribute("folder", folder);
			modelMap.addAttribute("article", article);
			
			List<ArticleVo> articles = fileService.getArticleListByFolderId(folder.getFolderId());//该目录下的所有文章
//			 List<Folder> parentsFolders=findAllParentsFolder(folder.getPath());//该目录的所有父级目录对象集合，用于显示“当前位置”
			 Article prevArticle=null;//上一篇
			 Article nextArticle=null;//下一篇
			 for(int i=0;i<articles.size();i++)
			 {
				 if(articles.get(i).getArticleId()==articleId)
					 {if(i>0)
					 prevArticle=articles.get(i-1);
				 if(i<articles.size()-1)
					 nextArticle=articles.get(i+1);}
				 if(prevArticle!=null && nextArticle!=null)
					 break;
			 }
			 
			 modelMap.put("prevArticle", prevArticle);
			 modelMap.put("nextArticle", nextArticle);
			
			modelMap.addAttribute("g_folderId", folderService.firstFolderId(folder.getFolderId()));//当前选中的顶级分类
			List<FolderVo> secondGradeMenus=folderService.getFolderListByFatherId(Long.valueOf(folder.getPath().split("#")[0]),FolderConstant.status.display);//二级导航菜单
			Folder ancestorFolder=folderService.getFolderById(Long.valueOf(folder.getPath().split("#")[0]));//获得该文章的顶级目录 (祖先目录)
			modelMap.addAttribute("secondGradeMenus", secondGradeMenus);
			modelMap.addAttribute("ancestorFolder", ancestorFolder);
//			System.out.println(themeService.getArticleTemplate(article.getFolderId(),
//					articleId));
			return themeService.getArticleTemplate(article.getFolderId(),
					articleId);
		} catch (Exception e) {
			modelMap.addAttribute("g_folderId", 0);
			return themeService.get404();
		}
	}
	/**   
	* 描述：   通过给定path查询所有祖先目录
	* 创建人：YangJ  邮箱:yangjie@sxw100.com   
	* 创建时间：2015年6月8日 15:35:50  
	* 修改备注：   
	* @version    
	*    
	*/ 
	private List<Folder> findAllParentsFolder(String path ){
		String[] folderIds=path.split("#");
		List<Folder> list=new ArrayList<Folder>();
		if(folderIds.length>1)
		for(int i=0;i<folderIds.length-1;i++)
			try {
				list.add(folderService.getFolderById(Long.valueOf(folderIds[i])));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (FolderNotFoundException e) {
				e.printStackTrace();
			}
		return list;
	}
}
